package com.System.service.UserImpl;

import com.System.Dao.UserDao;
import com.System.pojo.*;
import com.System.service.User;
import com.System.untils.SqlSessionFactoryUtils;
import com.cloudHouse.session.SqlSession;
import com.cloudHouse.session.SqlSessionFactory;

import java.util.List;

public class UserService implements User {
    SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<User_Bar_Relationship> selectFocusRelation(com.System.pojo.User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User_Bar_Relationship> userBarRelationship = mapper.selectFocusRelation(user);
        sqlSession.close();
        return userBarRelationship;
    }

    @Override
    public Bar selectFocus(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Bar bar = mapper.selectFocus(userBarRelationship);
        sqlSession.close();
        return bar;
    }

    @Override
    public Bar selectBar(Bar bar) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Bar bar1 = mapper.selectBar(bar);
        sqlSession.close();
        return bar1;
    }

    @Override
    public User_Bar_Relationship selectRelationship(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User_Bar_Relationship userBarRelationship1 = mapper.selectRelationship(userBarRelationship);
        sqlSession.close();
        return userBarRelationship1;
    }

    @Override
    public void addBar(Bar bar) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.addBar(bar);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addRelationship(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.addRelationship(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Support> selectAllMySupport(com.System.pojo.User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<Support> supports = mapper.selectAllMySupport(user);
        sqlSession.close();
        return supports;
    }

    @Override
    public Comment selectMyLikeComment(Support support) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Comment comment = mapper.selectMyLikeComment(support);
        sqlSession.close();
        return comment;
    }

    @Override
    public List<Bar> selectHotPosts() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<Bar> bars = mapper.selectHotPosts();
        sqlSession.close();
        return bars;
    }

    @Override
    public List<Post> selectAllPosts() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<Post> posts = mapper.selectAllPosts();
        sqlSession.close();
        return posts;
    }

    @Override
    public void registerUser(com.System.pojo.User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.registerUser(user);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public com.System.pojo.User selectTarget(com.System.pojo.User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        com.System.pojo.User result = mapper.selectTarget(user);
        sqlSession.close();
        return result;
    }
}
